package cn.gjxblog.nio;

import org.junit.Test;

import java.nio.ByteBuffer;

/**
 * mark ： 标记，表示记录当前position的位置,可以通过rest（） 恢复到mark的位置
 *
 * 0<=mark <=position <= limit <= capacity
 *
 * 五 直接缓冲区与非直接缓冲区
 * 非直接缓冲区： 通过allocate() 方法分配缓冲区，将缓冲区建立在jvm的内存中（堆内）
 * 直接缓冲区： 通过allocateDirect() 方法分配直接缓冲区，将缓冲区建立物理内存中（堆外内存）
 *  缺点 ：分配和销毁耗资源大
 *      : 不易控制
 *
 *
 * @author gjx
 * @version v1.0
 * @Description
 * @date 2019/7/24 8:57
 */
public class TestBuffer {

    @Test
    public void test3(){
        //分配直接缓冲区
        ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
        System.out.println(buffer.isDirect());
    }

    @Test
    public void test2(){
        String str = "abcde";
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        buffer.put(str.getBytes());
        buffer.flip();

        byte[] bytes = new byte[buffer.limit()];
        buffer.get(bytes,0,2);

        System.out.println(new String(bytes,0,2));
        System.out.println("position: "+ buffer.position());


        //mark() 标记
        buffer.mark();

        buffer.get(bytes,2,2);
        System.out.println("position: "+ buffer.position());

        //reset(); 恢复到mark的位置
        buffer.reset();
        System.out.println("position: "+ buffer.position());

        //判断缓冲区是否还有剩余数据
        if(buffer.hasRemaining()){
            System.out.println(buffer.remaining());
        }
    }


    @Test
    public void test1(){
        //1 分派一个指定大小的缓冲区
        ByteBuffer buffer = ByteBuffer.allocate(1024);

        System.out.println("-------------------allocate---------------");
        System.out.println("position: "+ buffer.position());
        System.out.println("capacity: "+ buffer.capacity());
        System.out.println("limit: "+ buffer.limit());

        //2 利用 put（） 存储数据到缓冲区
        String str = "abc";
        buffer.put(str.getBytes());
        System.out.println("-----------put-----------------");
        System.out.println("position: "+ buffer.position());
        System.out.println("capacity: "+ buffer.capacity());
        System.out.println("limit: "+ buffer.limit());

        //3 切换读取数据模式
        buffer.flip();
        System.out.println("-----------flip-----------------");
        System.out.println("position: "+ buffer.position());
        System.out.println("capacity: "+ buffer.capacity());
        System.out.println("limit: "+ buffer.limit());

        byte[] bytes = new byte[buffer.limit()];

        //4 利用get（） 读取缓冲区中的数据
        buffer.get(bytes);
        System.out.println(new String(bytes));
        System.out.println("-----------get----------------");
        System.out.println("position: "+ buffer.position());
        System.out.println("capacity: "+ buffer.capacity());
        System.out.println("limit: "+ buffer.limit());

        //5 rewind() 可重复读
        buffer.rewind();
        System.out.println("-----------rewind----------------");
        System.out.println("position: "+ buffer.position());
        System.out.println("capacity: "+ buffer.capacity());
        System.out.println("limit: "+ buffer.limit());

        //6 clear() 清空缓冲区 ,但是缓冲区中的数据依然存在，但是处于“被遗忘”状态。
        buffer.clear();
        System.out.println("-----------clear----------------");
        System.out.println("position: "+ buffer.position());
        System.out.println("capacity: "+ buffer.capacity());
        System.out.println("limit: "+ buffer.limit());


        System.out.println((char)buffer.get());
    }
}
